﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.BaseControl.Selection;
using jSLCore.Client.Presentation.jBinding.BaseConverters;

namespace jSLApp.Client.Forms
{
    public partial class NewMessage : DialogControl
    {
        private readonly string _toLogin;
        private readonly string _name;
        private readonly int? _sourceMessageId;

        public NewMessage(string toLogin,string name):this()
        {
            _toLogin = toLogin;
            _name = name;
        }

        public NewMessage(int sourceMessageId):this()
        {
            _sourceMessageId = sourceMessageId;
        }

        public NewMessage()
        {
            InitializeComponent();
        }

        public override void LoadInitialData()
        {
            if (!string.IsNullOrEmpty(_toLogin) && !string.IsNullOrEmpty(_name))
            {
                recLink.Tag = _toLogin;
                recLink.Content = _name;
                Bind();
                InvokeFirstErrorChanged(EventArgs.Empty);
            }
            if(_sourceMessageId!=null)
            {
                DoWork("Загрузка сообщения",(sender, args) =>
                                                {
                                                    args.Result = Communication.Proxy.GetUserMessage(args.Int);
                                                },(o, eventArgs) =>
                                                                      {
                                                                          if(eventArgs.Error==null)
                                                                          {
                                                                              var message = (Message)eventArgs.Result;
                                                                              string addText;
                                                                              var converter = new jFormatDateTimeConverter();
                                                                              var dt =
                                                                                 converter.Convert(
                                                                                     message.CreationDate,
                                                                                     typeof(string), null,
                                                                                     CultureInfo.CurrentCulture);

                                                                              if (jSecurity.Login == message.To.Trim())
                                                                              {
                                                                                  recLink.Content = message.FromText;
                                                                                  recLink.Tag = message.From.Trim();
                                                                                 
                                                                              }
                                                                              else
                                                                              {
                                                                                  recLink.Content = message.ToText;
                                                                                  recLink.Tag = message.To.Trim();
                                                                                 // addText = message.ToText + " " + dt + ": ";
                                                                              }

                                                                              addText = message.FromText + " " + dt + ": ";
                                                                              
                                                                              
                                                                              txt.InsertReplyText(addText,message.Text);

                                                                              Bind();
                                                                              InvokeFirstErrorChanged(EventArgs.Empty);
                                                                          }
                                                                      },_sourceMessageId);
            }
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        #region dialog

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override bool AutoSize
        {
            get { return true; }
        }

        public override object OkContent
        {
            get { return "Отправить"; }
        }


        #endregion

        public override void OkClicked()
        {
            DoWork("Отправка сообщения",(sender, args) =>
                                            {
                                                Communication.Proxy.SendMessage(args.String,(string) args.Params[1]);
                                            },(o, eventArgs) =>
                                                                  {
                                                                      if(eventArgs.Error==null)
                                                                      {
                                                                          InfoBox.ShowDialog("Сообщение отправилено",2000,this);
                                                                          InvokeDataSelected();
                                                                      }
                                                                  },recLink.Tag,txt.jSLGetText());
        }

        private void pointTo_Recepient(object sender, RoutedEventArgs e)
        {
            var keep = txt.jSLGetText();
            var dlg = new SimpleUserList() {SelectionMode = GridSelectionMode.SingleRow};
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var user = (User)data;
                                   recLink.Tag = user.Login;
                                   recLink.Content = user.NameText;
                                   txt.jSLSetText(keep);
                               },this );

        }
    }
}
